// pages/my/my.js
var app = getApp()
Page({
  data: {
    goodsList:[],
    goodsPriceShow:true,
    goodsInfoShow:true,
    // coupon:'50',
    settlementPrice:'',
    name:'',
    phone:'',
    address:'',
    addressId:'',
    transportTypeList:['卖家配送','第三方物流','自提'],
    transportType:'卖家配送',
    orderId:'',
    note:''
  },
  onLoad: function (options) {
    app.getMemInfo().then(res=>{
      this.getAddress(res.data.agentId)
      this.setData({
        agentId:res.data.agentId
      })
    })
    var data = options.orderId
    this.setData({
      orderId:data
    })
    this.getOrderDetail(data)

  },
  getOrderDetail(orderId){
    var that = this
    var data = {
      orderId
    }
    app.request('wx_app/agent_order/queryOrderInfoById',data,'GET').then(res=>{
      console.log(res)
      let goodsList = []
      res.data.orderDetails.forEach(item=>{
        goodsList.push({
          name:item.name,
          url:item.pic,
          price:item.price,
          type:item.versionValueDesc,
          num:item.amount
        })
      })
      that.setData({
        goodsList,
        labor_cost:res.data.labor_cost?res.data.labor_cost:0,
        order_no:res.data.orderNo,
        order_time:res.data.createTime,
        product_price:res.data.productPrice,
        order_price:res.data.orderPrice,
        status:res.data.orderState
      })
    })
  },
  saveNote(e){
    this.setData({
      note:e.detail.value
    })
  },
  bindApply(){
    if (this.data.addressId) {    
      let data = {
        orderId:this.data.orderId,
        logisticsType:this.data.transportType,
        note:this.data.note,
        addressId:this.data.addressId
      }
      console.log(data)
      app.request('wx_app/agent_order/apply_save',data,'GET').then(res=>{
        console.log(res)
        if (res.code==1) {
          wx.showToast({
            icon:'success',
            title:'申请成功'
          })
          setTimeout(function(){
            wx.switchTab({
              url:'../../my/my'
            })
          },1500)
        }else{
          app.errorMsg(res.message)
        }
      })
    }else{
      wx.showToast({
        icon:'none',
        title:'请选择收货地址'
      })
    }
  },
  collectAddress(){
    wx.navigateTo({
      url:'../address/address?type=1'
    })
  },
  choseTransType(e){
    console.log(e.detail.value)
    this.setData({
      transportType:this.data.transportTypeList[e.detail.value]
    })
  },
  getAddress(agentId){
    let data = {
      agentId
    }
    app.request('wx_app/agent/address/list',data,'GET').then(res=>{
      console.log(res.data[0])
      this.setData({
        address:res.data[0].address,
        name:res.data[0].contactName,
        phone:res.data[0].mobilePhone,
        addressId:res.data[0].id
      })
    })
  }
})